import pygame

import character
import data
class Bullet(pygame.sprite.Sprite):
    def __init__(self, origin, vel, image):
        pygame.sprite.Sprite.__init__(self)

        self.image, self.rect = data.load_image(image, -1)

        #real coordinates, we keepp self.rect for itself, just in case:
        
        self.world_pos = pygame.Rect(origin, self.rect.size)
        self.world_pos.center = origin
        
        self.vel = vel
                    
    def update(self, camera, curr_level = None, t=1):
        """Update position according to speed, and check for collisions.
        Also prepare for drawing, by using camera to calculate screen coordinates"""
        
        self.world_pos.move_ip(t*self.vel[0], t*self.vel[1])  

                
        #React to level environment
        if curr_level:
            #if self.outside(curr_level): #player has fallen to death
            #    self.die()                            # and must be terminated!!!
            #    sys.exit(0)
                
            #This also uses self.rect... We recalculate other values as well, so we shouldn't mind
            self.rect.center = self.world_pos.center
            tiles = pygame.sprite.spritecollide(self, curr_level.tile_group, False)

            if tiles:
                self.kill()
                return
                    
        # We need to convert the world corrdinates to screen coords, for drawing
        x = ((self.world_pos.x - camera.x))# / self.coord_scale)
        y = ((self.world_pos.y - camera.y))# / self.coord_scale)
        
        self.rect.topleft = (x, y)
